"""
@name : codechange
@author : wenyao
@projectname: sanchuanglianxi
"""
#unicode万国码  所有语言所有的字符都给他编了个号
#ascii码        对英文字符进行一个编号

#编码即为编码  是人类语言和机器语言的一种映射关系

#unicode编码方式 utf-8、 utf-16、 utf-32
#英文占一个字节 中文占三个字节
#10101100 10101100 10101100
#你好  一个字符三个字节 要用24位二进制去表示一个中文字符

#gbk gb2312
#每个字符占两个字节
# >>> str1 = "你好"
# >>> str1.encode("utf-8")
# b'\xe4\xbd\xa0\xe5\xa5\xbd'
# >>> bin(0xe4)
# '0b11100100'
# >>> str1.encode("gbk")
# b'\xc4\xe3\xba\xc3'
# >>> str1.encode("gbk").decode("gbk")
# '你好'
# >>> str1.encode("gbk").decode("utf-8")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# UnicodeDecodeError: 'utf-8' codec can't decodebyte 0xc4 in position 0: invalid continuation byte

#不同编码方式打开
# >>> fp = open("utf.txt")
# >>> fp.read()
# '\ufeff这是utf8'
# >>> fp = open("utf.txt",encoding = "uft-8-sig")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# LookupError: unknown encoding: uft-8-sig
# >>> fp = open("utf.txt",encoding = "utf-8-sig")
# >>> fp.read()
# '这是utf8'
# >>> fp = open("gbk.txt")
# >>> fp.read()
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/usr/lib64/python3.6/codecs.py", line 321, in decode
#     (result, consumed) = self._buffer_decode(data, self.errors, final)
# UnicodeDecodeError: 'utf-8' codec can't decodebyte 0xd5 in position 0: invalid continuation byte
# >>> fp = open("gbk.txt",encoding = "gbk")
# >>> fp.read()
# '这是gbk'


#检测文件编码
# pip3 install chardet
# >>> import chardet
# >>> f = open('utf-8.txt','rb')
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# FileNotFoundError: [Errno 2] No such file or directory: 'utf-8.txt'
# >>> f = open('utf.txt','rb')
# >>> chardet.detect(f.read())
# {'encoding': 'UTF-8-SIG', 'confidence': 1.0, 'language': ''}

#读模式  文件不存在就报错  默认模式 “r”
# >>> f = open('test1.txt','r')
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: close() takes no arguments (1 given)
#只读模式不支持写操作
# >>> f = open('test1.txt',"r")
# >>> f.write("123456")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# io.UnsupportedOperation: not writable

#写模式  默认缓冲区满 或者是 文件关闭才会把数据刷到磁盘上去，文件不存在会直接新建，
#以覆盖方式写入
# >>> f = open('test1.txt','w')
# >>> f.write("abc")
# 3
# >>> f.close()
# >>> f = open('test1.txt','w')
# >>> f.write("abcdefghij")
# 10
# >>> f.close()

#x  创建新文件  文件如果存在就报错
# >>> f = open("test1.txt","x")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# FileExistsError: [Errno 17] File exists: 'test1.txt'
# >>> f = open("test2.txt","x")
# >>> f.write("12345")
# 5
# >>> f.close()

#a  追加到文件末尾，不直接覆盖
# >>> f = open("test2.txt","a")
# >>> f.write("xixi\nhaha")
# 9
# >>> f.close()

#r+  可读写，追加到文件末尾， 文件没有就报错

# >>> f = open("test2.txt","r+")
# >>> f.read()
# '12345xixi\nhaha'
# >>> f.write("abcefg")
# 6
# >>> f.close()

# +  可读写  r+  w+ a+ x+

#用with的方式打开文件， with会帮你去管理 这个文件close的情况
# >>> with open("test2.txt","w+") as f1:
# ...    f1.write("with open type")
# ...
# 14

# f = open("test.txt","rb")
# print("输出光标当前位置",f.tell())
# print("读取内容：",f.read(10))
# print("输出光标当前位置",f.tell())
# print("第二次读取:",f.read(10))
# print("第二次读取完成后,当前位置：",f.tell())
# f.seek(-12,2)
# print("第三次读取：",f.read().decode("utf-8"))
# f.close()

# f = open("test.txt","r", encoding="utf-8")
# print(f.readlines())
# f.close()

# f = open("test.txt","r", encoding="utf-8")
# print(f.readline())
# f.close()

# f = open("test1.txt", "w+" ,encoding="utf-8")
# f.write("123\n")

#flush  close 数据会从缓冲区写入磁盘
# >>> f = open("test3.txt","w+")
# >>> f.write("123\n")
# 4
# >>> f.flush()
# >>> f.write("456\n")
# 4
# >>> f.close()

#buffering设置
## 0 只用于二进制模式  实时存放
#  1  文本模式 行缓存 遇到\n换行符就自动把缓存数据刷新到磁盘
# 其他正整数  n  设定缓冲区大小为n*4096  缓冲区满，才会把内容刷到磁盘上保存

# >>> f = open("test3.txt","w+",buffering = 1)
# >>> f.write("abc")
# 3
# >>> f.write("\n")
# 1
# >>> f = open("test3.txt","w+",buffering = 2)
# >>> f.write("a"**4096)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
# >>> f.write("a"*4096)
# 4096
# >>> f.write("a"*4096)
# 4096
# >>> f.write("a")
# 1
# >>> #f = open("test3.txt","w+",buffering = 0)
# ... f
# <_io.TextIOWrapper name='test3.txt' mode='w+' encoding='UTF-8'>
# >>>
# >>> f.close()
# >>> f = open("test3.txt","wb",buffering = 0)
# >>> f.write("abc")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: a bytes-like object is required, not 'str'
# >>> f = open("test3.txt","wb",buffering = 0)
# >>> f.write(b"abc")
# 3
#
# f = open("test.txt","r+")
# # print("文件可读吗？",f.readable())
# # print("文件可写吗？",f.writable())
# f.truncate(15)

#生成随机ip段
# import random
# f = open('ips.txt','w+')
# for i in range(1200):
#     str1 = '172.25.254.'
#     str2 = str(random.randint(1,255))
#     str3 = str1 + str2
#     f.write(str3 + '\n')
# f.seek(0,0)   ##将指针指向0
# s = {}
# for i in f.readlines():
#     if i in s:
#         s[i] += 1
#     else:
#         s[i] = 1
# sort_li = list(s.items())
# #print(sort_li)
# #[('172.25.254.47\n', 5), ('172.25.254.202\n', 4), ('172.25.254.179\n', 4)]
# sort = sorted(sort_li,key=lambda x:x[1],reverse = True)
# print(sort)
# for i in sort[:10]:
#     print(f"{i[0]}出现的次数是{i[1]}次")
# f.close()

#生成随机mac地址
# import random
# import string

# def CreateMac():

#     # MAC地址的长度为48位(6个字节),通常表示为12个16进制数
#     MAC = '01-AF-3B'

#     # string.hexdogits 输出所有16进制数字字符串
#     HexNum = string.hexdigits # Or : HexNum = '0123456789abcdefABCDEF' except import string

#     for i in range(3):

#         # sample(A, Num): 从序列A中随机抽取Num个元素，并将Num个元素生以list形式返回。

#         n = random.sample(HexNum,2)

#         sn = '-' + ''.join(n).upper()

#         MAC += sn

#     return MAC

# if __name__ == "__main__":

#     with open('mac.txt', 'w', encoding='utf-8') as f:

#         for i in range(100):

#             mac = CreateMac()

#             print(mac)

#             # 写入
#             f.write(mac + '\n')



